﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.Account.Model;
using Yipusoft.Account.BLL;
using Yipusoft.SoldierExam.Model;
using Yipusoft.SoldierExam.BLL;
using Yipusoft.SoldierArchives.BLL;
using Yipusoft.SoldierArchives.Model;
using Yipusoft.Common.Model;
using Yipusoft.Common.BLL;

public partial class Aptitude_AptitudeScore : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            lblNote.Text = AptitudeRanger.GetRangerById(int.Parse(Request["RangerID"])).Note;
            dlScore.DataSource = AptitudeItem.GetListBySkill("ZZKH010100");
            dlScore.DataBind();

            dlStand.DataSource = AptitudeItem.GetListBySkill("ZZKH010200");
            dlStand.DataBind();

            bool isContract = false;
            if (Request.QueryString["Contract"] == "1")
            {
                isContract = true;
            }
            List<AptitudeSoldierInfo> soldierList = AptitudeSoldier.GetNoExamSoldiers(Request["DepartmentID"], int.Parse(Request["RangerID"]), int.Parse(Request["ExamID"]), isContract);
            int count=soldierList.Count;
            if (count == 0)
            {
                Response.Write("<script>alert('人员个数为0！');history.back(0);</script>");
                return;
            }
            ddlSoldiers.DataSource = soldierList;
            ddlSoldiers.DataTextField = "SoldierName";
            ddlSoldiers.DataValueField = "SoldierID";
            ddlSoldiers.DataBind();

            BindSoldier();
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        foreach (DataListItem item in dlScore.Items)
        {
            TextBox result = item.FindControl("Result") as TextBox;
            TextBox score = item.FindControl("Score") as TextBox;
            HiddenField itemID = item.FindControl("ItemID") as HiddenField;
            AptitudeExamScoreInfo examScore = new AptitudeExamScoreInfo();
            examScore.ExamDate = ExamDate.Text == "" ? DateTime.Today : Convert.ToDateTime(ExamDate.Text);
            examScore.ExamID = int.Parse(Request["ExamID"]);
            examScore.ExamLeader = ExamLeader.Text;
            examScore.ExamMember = ExamMember.Text;
            examScore.InputDate = DateTime.Today;
            examScore.InputUser = User.Identity.Name;
            examScore.ItemID = int.Parse(itemID.Value);
            examScore.RangerID = int.Parse(Request["RangerID"]);
            examScore.Result = result.Text;
            examScore.Score = score.Text == "" ? 0.0m : Convert.ToDecimal(score.Text);
            examScore.SoldierID = new Guid(ddlSoldiers.SelectedValue);
            examScore.Standard = 0;
            int id = AptitudeExamScore.Insert(examScore);
            EventLogInfo log = new EventLogInfo();
            log.EventTime = DateTime.Now;
            log.EventType = 1;
            log.EventData = "添加一个体能考核成绩";
            log.ItemID = id.ToString();
            log.ItemName = "Combat";
            log.ItemFullUrl = "Aptitude/AptitudeScore.aspx";
            log.CreatedBy = Context.User.Identity.Name;
            log.CreatedTime = DateTime.Now;
            log.UserIP = Request.UserHostAddress;
            EventLog.Insert(log);

        }

        foreach (DataListItem item in dlStand.Items)
        {
            TextBox result = item.FindControl("Result") as TextBox;
            DropDownList stanard = item.FindControl("ddlStand") as DropDownList;
            HiddenField itemID = item.FindControl("ItemID") as HiddenField;
            AptitudeExamScoreInfo examScore = new AptitudeExamScoreInfo();
            examScore.ExamDate = ExamDate.Text == "" ? DateTime.Today : Convert.ToDateTime(ExamDate.Text);
            examScore.ExamID = int.Parse(Request["ExamID"]);
            examScore.ExamLeader = ExamLeader.Text;
            examScore.ExamMember = ExamMember.Text;
            examScore.InputDate = DateTime.Today;
            examScore.InputUser = User.Identity.Name;
            examScore.ItemID = int.Parse(itemID.Value);
            examScore.RangerID = int.Parse(Request["RangerID"]);
            examScore.Result = result.Text;
            examScore.Score = -1.0m;
            examScore.SoldierID = new Guid(ddlSoldiers.SelectedValue);
            examScore.Standard = int.Parse(stanard.SelectedValue);
            int id = AptitudeExamScore.Insert(examScore);
            EventLogInfo log = new EventLogInfo();
            log.EventTime = DateTime.Now;
            log.EventType = 1;
            log.EventData = "添加一个技能考核成绩";
            log.ItemID = id.ToString();
            log.ItemName = "Combat";
            log.ItemFullUrl = "Aptitude/AptitudeScore.aspx";
            log.CreatedBy = Context.User.Identity.Name;
            log.CreatedTime = DateTime.Now;
            log.UserIP = Request.UserHostAddress;
            EventLog.Insert(log);
        }

        AptitudeExamScoreInfo examScoreInfo = new AptitudeExamScoreInfo();
        examScoreInfo.ExamDate = ExamDate.Text == "" ? DateTime.Today : Convert.ToDateTime(ExamDate.Text);
        examScoreInfo.ExamID = int.Parse(Request["ExamID"]);
        examScoreInfo.ExamLeader = ExamLeader.Text;
        examScoreInfo.ExamMember = ExamMember.Text;
        examScoreInfo.InputDate = DateTime.Today;
        examScoreInfo.InputUser = User.Identity.Name;
        examScoreInfo.ItemID = 13;
        examScoreInfo.RangerID = int.Parse(Request["RangerID"]);
        examScoreInfo.Result = "";
        examScoreInfo.Score = Convert.ToDecimal(ExamScore.Text == "" ? "0" : ExamScore.Text);
        examScoreInfo.SoldierID = new Guid(ddlSoldiers.SelectedValue);
        examScoreInfo.Standard = 0;
        int id1 = AptitudeExamScore.Insert(examScoreInfo);

        EventLogInfo log1 = new EventLogInfo();
        log1.EventTime = DateTime.Now;
        log1.EventType = 1;
        log1.EventData = "添加一个笔试考核成绩";
        log1.ItemID = id1.ToString();
        log1.ItemName = "Combat";
        log1.ItemFullUrl = "Aptitude/AptitudeScore.aspx";
        log1.CreatedBy = Context.User.Identity.Name;
        log1.CreatedTime = DateTime.Now;
        log1.UserIP = Request.UserHostAddress;
        EventLog.Insert(log1);

        int standarID = AptitudeStandard.Insert(int.Parse(Request["RangerID"]), int.Parse(Request["ExamID"]), new Guid(ddlSoldiers.SelectedValue));
        log1.EventTime = DateTime.Now;
        log1.EventType = 1;
        log1.EventData = "添加一个考核结果";
        log1.ItemID = standarID.ToString();
        log1.ItemName = "Combat";
        log1.ItemFullUrl = "Aptitude/AptitudeScore.aspx";
        log1.CreatedBy = Context.User.Identity.Name;
        log1.CreatedTime = DateTime.Now;
        log1.UserIP = Request.UserHostAddress;
        EventLog.Insert(log1);
        if (standarID > 0)
        {
            btnSave.Visible = false;
            panlPD.Visible = true;
            AptitudeStandardInfo StandardInfo = AptitudeStandard.GetStandardById(standarID);
            lblSumScore.Text = StandardInfo.PhysicalAvgScore.ToString();
            hfStandardID.Value = StandardInfo.StandardID.ToString();
            if (StandardInfo.SkillStandard)
            {
                lblStandard.Text = "技能考核全部达标";
                lblStandard.ForeColor = System.Drawing.Color.Black;

                AptitudeRangerInfo ranger = AptitudeRanger.GetRangerById(int.Parse(Request["RangerID"]));
                if (StandardInfo.PhysicalAvgScore >= ranger.TiNeng && Convert.ToDecimal(ExamScore.Text == "" ? "0" : ExamScore.Text) >= ranger.ExamScore)
                {
                    StandardInfo.Standard = 1;
                    lblPD.Text = "合格";
                }
                else
                {
                    StandardInfo.Standard = -1;
                    lblPD.Text = "不合格";
                }
            }
            else
            {
                lblStandard.Text = "技能考核没有全部达标";
                lblStandard.ForeColor = System.Drawing.Color.Red;
                //ddlZonghe.SelectedValue = "-1";
                StandardInfo.Standard = -1;
                lblPD.Text = "不合格";
            }
            if (AptitudeStandard.UpdateAvgScore(StandardInfo))
                btnPD.Visible = true;
        }
    }
    protected void btnPD_Click(object sender, EventArgs e)
    {
        Response.Redirect("aptitudeScore.aspx?DepartmentID=" + Request["DepartmentID"] + "&RangerID=" + Request["RangerID"] + "&ExamID=" + Request["ExamID"]);
    }
    protected void ddlSoldiers_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindSoldier();
    }

    private void BindSoldier()
    {
        string SoldierId = ddlSoldiers.SelectedValue;
        Soldier soldier = new Soldier();
        SoldierArchiveInfo soldierInfo = soldier.GetSoldierInfoByID(SoldierId);
        lblDepartmentName.Text = soldierInfo.DepartmentName;
        lblRander.Text = soldierInfo.RankName;
    }
}
